import { filterObj } from '@/utils/utils';
import { preview, deletePolicy, download } from '@/api/zcpd';

const query = {
  data: {

  },
  'pageNo': 0,
  'pageSize': 0
};
export const mixins = {
  computed: {
    items() {
      return this.$route.meta.titles || [];
    }
  },
  filters: {
    unitWan(value) {
      return value + ' 万元';
    }
  },
  data() {
    return {
      dataSource: [],
      statusName: {
        1: '通过',
        0: '不通过'
      },
      status: {
        1: {
          label: '审核中',
          color: '#D9D9D9'
        },
        2: {
          label: '已过审',
          color: '#1890FF'
        },
        3: {
          label: '待审核',
          color: 'purple'
        },
        4: {
          label: '已完成',
          color: '#87d068'
        }
      },
      loading: false,
      ipagination: {
        current: 1,
        pageSize: 10,
        pageSizeOptions: ['2', '5', '10', '20', '30'],
        showTotal: (total, range) => {
          return range[0] + '-' + range[1] + ' 共' + total + '条';
        },
        showQuickJumper: true,
        showSizeChanger: false,
        total: 0
      },
      queryParam: JSON.parse(JSON.stringify(query))
    };
  },
  mounted() {
    this.loadData();
  },
  methods: {
    encodeWithUri: s => window.encodeURIComponent(s),
    reset() {
      this.queryParam.data = {};
      this.queryParam.time = [];
      this.searchQuery();
    },
    handleTabChange(key) {
      if (key == '全部') {
        this.queryParam.data.tag = '';
      } else {
        this.queryParam.data.tag = key;
      }
      this.ipagination.current = 1;
      this.ipagination.pageSize = 10;
      this.searchQuery();
    },
    searchQuery() {
      if (this.items[this.items.length - 1] == '项目名称') {
        this.taskTag();
      }
      this.loadData();
    },
    searchReset(query) {
      this.queryParam = JSON.parse(JSON.stringify(query));
      if (query) {
        this.queryParam.data = { ...query };
      }
      this.loadData(1);
    },
    handleDelete({ policyId }) {
      deletePolicy({ policyId }).then(res => {
        this.searchQuery();
      });
    },
    preview(file) {
      return preview(file);
    },
    download(file) {
      return download(file);
    },
    getRangTime(e, string) {
      this.queryParam.data.startTime = string[0];
      this.queryParam.data.endTime = string[1];
    },
    handleTableChange(pagination, filters, sorter) {
      console.log(pagination);
      this.ipagination = pagination;
      this.loadData();
    },
    loadData(arg) {
      if (!this.action || !this.action.list) return;
      // 加载数据 若传入参数1则加载第一页的内容
      if (arg === 1) {
        this.ipagination.current = 1;
      }
      var params = this.getQueryParams();// 查询条件
      this.loading = true;
      this.action.list(params).then((res) => {
        if (res.success) {
          const result = res.result.datas || res.result;
          this.dataSource = result.map(e => {
            return {
              ...e,
              effectFlagBool: !!e.effectFlag
            };
          });
          if (res.result.totalSize) {
            this.ipagination.total = res.result.totalSize;
          } else {
            this.ipagination.total = 0;
          }
        } else {
          this.$message.warning(res.message);
        }
      }).finally(() => {
        this.loading = false;
      });
    },
    getQueryParams() {
      // 获取查询条件
      let param = {};

      param = Object.assign({}, this.queryParam);
      // param.field = this.getQueryField();

      param.pageNo = this.ipagination.current;
      param.pageSize = this.ipagination.pageSize;
      return filterObj(param);
    },
    handleEdit() {

    },
    handleLook() {

    }
  }
};
